﻿using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;

namespace CustomWindowTitle.Controls
{
    /// <summary>
    /// TitleControl.xaml 的交互逻辑
    /// </summary>
    public partial class TitleControl : UserControl
    {

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(TitleControl), new FrameworkPropertyMetadata(string.Empty));


        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set
            {
                SetValue(TitleProperty, value);
            }
        }
        private Window parentWindow;

        private const string btnMaxStyleKey = "btnMax";
        private const string btnResStyleKey = "btnRes";
        /// <summary>
        /// 父窗体对象
        /// </summary>
        public Window? ParentWindow
        {
            get
            {
                if (parentWindow == null)
                {
                    DependencyObject parent = VisualTreeHelper.GetParent(this);
                    while (parent != null)
                    {
                        if (parent is Window)
                        {
                            parentWindow = parent as Window;
                            return parentWindow;
                        }
                        parent = VisualTreeHelper.GetParent(parent);
                    }
                }
                return parentWindow;
            }
        }
        public TitleControl()
        {
            InitializeComponent();
        }

        #region 标题栏按钮事件
        private void btnMaximizationClick()
        {
            btnMax_Click(btnMax, null);
        }

        private void btnMin_Click(object sender, RoutedEventArgs e)
        {
            if (ParentWindow != null)
                ParentWindow.WindowState = WindowState.Minimized;
        }
        private void btnMax_Click(object sender, RoutedEventArgs e)
        {
            if (ParentWindow == null)
                return;
            if (ParentWindow.WindowState == WindowState.Normal)
            {
                ParentWindow.WindowState = WindowState.Maximized;
                var style = (Style)this.FindResource(btnResStyleKey);
                btnMax.Style = style;
            }
            else if (ParentWindow.WindowState == WindowState.Maximized)
            {
                ParentWindow.WindowState = WindowState.Normal;
                var style = (Style)this.FindResource(btnMaxStyleKey);
                btnMax.Style = style;
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (ParentWindow != null)
                ParentWindow.Close();
        }
        #endregion

        #region 拖拽窗体

        bool isDragging = false;
        Point pressedPosition;
        protected void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            pressedPosition = e.GetPosition(this);
        }

        protected void Window_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed && pressedPosition != e.GetPosition(ParentWindow) && ParentWindow != null)
            {
                if (ParentWindow.WindowState == WindowState.Maximized)
                {
                    ParentWindow.WindowState = WindowState.Normal;
                    var p = e.GetPosition(ParentWindow);
                    double x = p.X / SystemParameters.WorkArea.Width * ParentWindow.Width;
                    ParentWindow.Top = p.Y - 20;
                    ParentWindow.Left = p.X - x;
                }
                isDragging = true;
                ParentWindow.DragMove();
            }
        }

        protected void Window_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDragging)
            {
                isDragging = false;
                e.Handled = true;
            }
        }
        #endregion

        #region 最大化样式处理
        private void WindowBase_StateChanged(object? sender, EventArgs e)
        {
            if (ParentWindow.WindowState == WindowState.Maximized)
            {
                ParentWindow.BorderThickness = new Thickness(0, 6, 0, 0);
                if (ParentWindow.Width >= SystemParameters.WorkArea.Width)
                    ParentWindow.MaxHeight = SystemParameters.WorkArea.Height + (ParentWindow.Width - SystemParameters.WorkArea.Width);
                else
                {
                    ParentWindow.MaxHeight = SystemParameters.WorkArea.Height + 14;
                }
                var style = (Style)this.FindResource(btnResStyleKey);
                btnMax.Style = style;
            }
            else
            {
                ParentWindow.BorderThickness = new Thickness(1);
                var style = (Style)this.FindResource(btnMaxStyleKey);
                btnMax.Style = style;
            }
        }
        #endregion

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (ParentWindow != null)
                ParentWindow.StateChanged += WindowBase_StateChanged;
            lbTitle.Content = Title;
        }

        private void gridTitle_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                btnMax_Click(btnMax, null);
            }
        }
    }
}
